home *** CD-ROM | disk | FTP | other *** search
/ Deutsche Edition 1 / Deutsche Edition 1.iso / amok / 051-060 / amok58 / sortedlists / sortedlists.mod < prev    next >
Text File  |  1993-11-04  |  3KB  |  102 lines

  1. (* ==================================================================== *)
  2. (* === SortedLists ==================================================== *)
  3. (* ==================================================================== *)
  4. (*
  5.  
  6.   :Program.       SortedLists.MOD
  7.   :Contents.      simple extension of Lists.MOD to allow sorted lists
  8.   :Author.        Peter Fröhlich [phf]
  9.   :Copyright.     Public Domain
  10.   :Language.      Oberon (revised)
  11.   :Translator.    Amiga Oberon V2.01
  12.   :History.       V0.1 [phf] 19-Aug-1991 [created]
  13.   :History.       V0.1 [phf] 30-Aug-1991 [last edit]
  14.  
  15.   :Support.       -
  16.   :Imports.       -
  17.   :Bugs.          none known, but implementation is quit inefficient
  18.  
  19.   :Address.       Z-NET:P.FROEHLICH@NEXT-BOX.ZER
  20.   :Remark.        This is a quick hack! Feel free to improve on
  21.   :Remark.        my code, but post any changes to the net, please.
  22.  
  23. *)
  24. (* ==================================================================== *)
  25.  
  26. MODULE SortedLists;
  27.  
  28. (* ==================================================================== *)
  29. (* === Imports ======================================================== *)
  30. (* ==================================================================== *)
  31.  
  32. IMPORT
  33.   li : Lists;
  34.  
  35. (* ==================================================================== *)
  36. (* === Types ========================================================== *)
  37. (* ==================================================================== *)
  38.  
  39. TYPE
  40.   CompProc * = PROCEDURE(a,b: li.NodePtr): INTEGER;
  41. (*
  42.   CompProc is used to compare two nodes. Its result should be:
  43.     < 0, if a < b
  44.     > 0, if a > b
  45.   or
  46.     = 0, if both nodes are equal
  47. *)
  48.  
  49. (* ==================================================================== *)
  50. (* === Procedures ===================================================== *)
  51. (* ==================================================================== *)
  52.  
  53. (* --- AddSorted ------------------------------------------------------ *)
  54. (*
  55.   :Semantic.      inserts a node at the proper location in a list
  56.   :Note.          quit inefficient but i see no other solutions
  57. *)
  58. PROCEDURE AddSorted*(VAR list: li.List; n: li.NodePtr; comp: CompProc);
  59. VAR
  60.    h : li.NodePtr;
  61.   ok : BOOLEAN;
  62. BEGIN
  63.   ok := FALSE;
  64.   IF li.Empty(list) THEN
  65.     li.AddHead(list,n);
  66.   ELSE
  67.     h := li.Head(list);
  68.     WHILE (h # NIL) DO
  69.       IF comp(h,n) >= 0 THEN
  70.         li.AddBefore(list,n,h); ok := TRUE;
  71.       END;
  72.       li.Succ(h);
  73.     END;
  74.     IF NOT ok THEN
  75.       li.AddTail(list,n);
  76.     END;
  77.   END;
  78. END AddSorted;
  79. (* -------------------------------------------------------------------- *)
  80.  
  81. (* --- Sort (not implemented) ----------------------------------------- *)
  82. (*
  83.   :Semantic.      (re-)sort a list
  84. *)
  85. PROCEDURE Sort(VAR list: li.List; comp: CompProc);
  86. BEGIN
  87.   IF NOT (li.Empty(list)) THEN
  88.     (* modify QuickSort ? *)
  89.   END;
  90. END Sort;
  91. (* -------------------------------------------------------------------- *)
  92. (* ==================================================================== *)
  93.  
  94. (* ==================================================================== *)
  95. (* === Main =========================================================== *)
  96. (* ==================================================================== *)
  97.  
  98. BEGIN (* SortedLists *)
  99. END SortedLists.
  100.  
  101. (* ==================================================================== *)
  102.